using System;
using System.Collections.Generic;
using System.Text;
using Decav.Gatsb.Data;
using Decav.Data;

namespace Decav.Gatsb
{

    /// <summary>
    /// Defines a location and time that a user updated their status to show that they were at
    /// a specific location.
    /// </summary>
    [Serializable]
    public class LocationCheckin
    {
        /// <summary>
        /// A <see cref="LocationCheckin"/> where no location or user is specified.
        /// </summary>
        public static readonly LocationCheckin Empty = new LocationCheckin(DateTime.MinValue, null, null);

        public LocationCheckin(DateTime checkinDate, Location location, User user)
        {
            if (location != null)
                _location = new LocationLazyLoadReference(location);
            else
                _location = new LocationLazyLoadReference(Guid.Empty);

            if (user != null)
                _user = new UserLazyLoadReference(user);
            else
                _user = new UserLazyLoadReference(Guid.Empty);

            _checkinDate = checkinDate;
            _id = Guid.NewGuid();
        }

        protected LocationCheckin(DataRecordAdapter adapt)
        {
            if (adapt == null)
                throw new ArgumentNullException("adapt");

            _checkinDate = adapt.GetDateTime("CheckinDate");
            _location = new LocationLazyLoadReference(adapt.GetGuid("LocationId"));
            _user = new UserLazyLoadReference(adapt.GetGuid("UserId"));
            _id = adapt.GetGuid("CheckinId");
        }

        private Guid _id;
        private DateTime _checkinDate;
        private LocationLazyLoadReference _location;
        private UserLazyLoadReference _user;

        /// <summary>
        /// Gets the unique identifier of the checkin.
        /// </summary>
        public Guid Id
        {
            get
            {
                return _id;
            }
        }

        /// <summary>
        /// Gets the user who checked in.
        /// </summary>
        public User User
        {
            get
            {
                return _user.Value;
            }
        }

        /// <summary>
        /// Gets the date and time the user checkedin to the location.
        /// </summary>
        public DateTime CheckinDate
        {
            get
            {
                return _checkinDate;
            }
        }

        /// <summary>
        /// Gets if the user has specified a location that they are at.
        /// </summary>
        public bool LocationSpecified
        {
            get
            {
                return _location.Id != Guid.Empty;
            }
        }

        /// <summary>
        /// Gets the location that the user checked into.
        /// </summary>
        public Location Location
        {
            get
            {
                return _location.Value;
            }
        }
    }
}
